'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data.status);
        this.expiryTime();
        this.toggleFreeze();
        this.changeCoach();
        this.changeGroup();
        this.tag();
        this.activate();
        this.changeKeyCoach();
        this.changeSite();
        this.stopCamp();
        this.honesty();
    },
    
    initPage: function(status){
    	$('select[name="site"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });

          $('select[name="site"]').change(function(){ 

            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/card-rounds?site=" + site + "&status=" + status;

          })
    },
    
      expiryTime: function() {
          $(document).on('shown.bs.modal', '#round-expiry-time,#round-delay', function(e){
        	  
                  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
                  var $form = $(e.currentTarget).find(".form-horizontal")                          //bind shiftform
                  $form.shiftform({
                    beforeSubmit : function(el) {
                      $('.form-horizontal').trigger("validate.ajax.submit")
                      return $.bt_validate.result[$.bt_validate.form_id]
                    }
                  })

          })
        },
        
      toggleFreeze : function() {
        $(document).on('shown.bs.modal', '#toggle-freeze', function(e){

        	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
			  
			  var $form = $(e.currentTarget).find("form")                          //bind shiftform
			  $form.shiftform({
				beforeSubmit : function(el) {
				  $form.trigger("validate.ajax.submit");
				  return $.bt_validate.result[$.bt_validate.form_id]
				}
			  })
          })
      },
      
      changeCoach: function(status) {
          $(document).on('shown.bs.modal', '#arrange-coach, #change-coach', function(e){

              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
      
     changeGroup: function(status) {
          $(document).on('shown.bs.modal', '#change-group, #edit-group', function(e){

              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
        
     tag: function(status) {
            $(document).on('shown.bs.modal', '#tag', function(e){

                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
            })
          },
          
      activate : function() {
          $(document).on('shown.bs.modal', '#round-activate', function(e){

        	  $(e.currentTarget).find("form").shiftform()
            })
        },
      
    
	  changeKeyCoach: function(status) {
		  $(document).on('shown.bs.modal', '#arrange-coach, #change-coach', function(e){

			  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
			  
			  var $form = $(e.currentTarget).find("form")                          //bind shiftform
			  $form.shiftform({
				beforeSubmit : function(el) {
				  $form.trigger("validate.ajax.submit");
				  return $.bt_validate.result[$.bt_validate.form_id]
				}
			  })
		  })
	},
    
	changeSite: function() {
        $(document).on('shown.bs.modal', '#round-change-site', function(e){
            $('select[name="siteId"]').on('change', function() {
                var siteId = $(this).val();
                
                $.ajax({
                    type: 'POST',
                    url: window.__CTX__.root + '/principals/coaches?siteId=' + siteId,
                    success: function(res) {
                        var coachSelect = '<select name="coachId" class="form-control select2" data-validate="required">'
                                         + '<option></option>';
                        res = JSON.parse(res);
                        for(var index in res.data.coaches) {
                            coachSelect = coachSelect + '<option value="' + res.data.coaches[index].id + '">' + res.data.coaches[index].name + '</option>';
                        }
                        coachSelect = coachSelect + '</select>';
                        $('.coach-item').html(coachSelect);
                        
                        $('select[name="coachId"]').select2({
                            placeholder : '请选择教练',
                            minimumResultsForSearch : -1,
                            allowClear : true
                        });
                        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()
                    }
                });
                
                
            });
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
      
      stopCamp: function(status) {
          $(document).on('shown.bs.modal', '#stop-camp', function(e){

        	  $(e.currentTarget).find("form").shiftform()
          })
      },
      
      honesty: function() {
    	  $(document).on('shown.bs.modal', '#honesty', function(e){

        	  $(e.currentTarget).find("form").shiftform()
          })
      }
  }
})
